// MegaVideo.js for PSPTube
// Made By JK108
// Decrypt Function By Emre Korkmaz

function MegaVideo_CheckURL( url, option )
{
	if (url.match("http://www.megavideo.com") == "http://www.megavideo.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a MegaVideo URL
*/
function MegaVideo_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var un = contents.match(/flashvars.un \= \"(.*?)\"/);
	var un = un[1];
	var k1 = contents.match(/flashvars.k1 \= \"(.*?)\"/);
	var k1 = k1[1];
	var k2 = contents.match(/flashvars.k2 \= \"(.*?)\"/);
	var k2 = k2[1];
	var s = contents.match(/flashvars.s \= \"(.*?)\"/);
	var s = s[1];
	return "http://www" + s + ".megavideo.com/files/" + Decrypt(un, k1, k2) + "/";
}

function Decrypt(str, key1, key2)
{
var _loc1 = [];
for (var _loc3 = 0; _loc3 < str.length; ++_loc3)
{
switch (str.charAt(_loc3))
{
case "0":
{
_loc1.push("0000");
break;
} 
case "1":
{
_loc1.push("0001");
break;
} 
case "2":
{
_loc1.push("0010");
break;
} 
case "3":
{
_loc1.push("0011");
break;
} 
case "4":
{
_loc1.push("0100");
break;
} 
case "5":
{
_loc1.push("0101");
break;
} 
case "6":
{
_loc1.push("0110");
break;
} 
case "7":
{
_loc1.push("0111");
break;
} 
case "8":
{
_loc1.push("1000");
break;
} 
case "9":
{
_loc1.push("1001");
break;
} 
case "a":
{
_loc1.push("1010");
break;
} 
case "b":
{
_loc1.push("1011");
break;
} 
case "c":
{
_loc1.push("1100");
break;
} 
case "d":
{
_loc1.push("1101");
break;
} 
case "e":
{
_loc1.push("1110");
break;
} 
case "f":
{
_loc1.push("1111");
break;
} 
} // End of switch
} // end of for
_loc1 = _loc1.join("").split("");
var _loc6 = [];
for (var _loc3 = 0; _loc3 < 384; ++_loc3)
{
key1 = (key1 * 11 + 77213) % 81371;
key2 = (key2 * 17 + 92717) % 192811;
_loc6[_loc3] = (key1 + key2) % 128;
} // end of for
for (var _loc3 = 256; _loc3 >= 0; --_loc3)
{
var _loc5 = _loc6[_loc3];
var _loc4 = _loc3 % 128;
var _loc8 = _loc1[_loc5];
_loc1[_loc5] = _loc1[_loc4];
_loc1[_loc4] = _loc8;
} // end of for
for (var _loc3 = 0; _loc3 < 128; ++_loc3)
{
_loc1[_loc3] = _loc1[_loc3] ^ _loc6[_loc3 + 256] & 1;
} // end of for
var _loc12 = _loc1.join("");
var _loc7 = [];
for (var _loc3 = 0; _loc3 < _loc12.length; _loc3 = _loc3 + 4)
{
var _loc9 = _loc12.substr(_loc3, 4);
_loc7.push(_loc9);
} // end of for
var _loc2 = [];
for (var _loc3 = 0; _loc3 < _loc7.length; ++_loc3)
{
switch (_loc7[_loc3])
{
case "0000":
{
_loc2.push("0");
break;
} 
case "0001":
{
_loc2.push("1");
break;
} 
case "0010":
{
_loc2.push("2");
break;
} 
case "0011":
{
_loc2.push("3");
break;
} 
case "0100":
{
_loc2.push("4");
break;
} 
case "0101":
{
_loc2.push("5");
break;
} 
case "0110":
{
_loc2.push("6");
break;
} 
case "0111":
{
_loc2.push("7");
break;
} 
case "1000":
{
_loc2.push("8");
break;
} 
case "1001":
{
_loc2.push("9");
break;
} 
case "1010":
{
_loc2.push("a");
break;
} 
case "1011":
{
_loc2.push("b");
break;
} 
case "1100":
{
_loc2.push("c");
break;
} 
case "1101":
{
_loc2.push("d");
break;
} 
case "1110":
{
_loc2.push("e");
break;
} 
case "1111":
{
_loc2.push("f");
break;
} 
}
}
return (_loc2.join(""));
}

function MegaVideo_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.megavideo.com/?c=search&s=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.megavideo.com/?c=search&s=" + keyword.replace(/ /g, "+") + "&p=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("</b> - did not match any videos. <br><br>") == "</b> - did not match any videos. <br><br>"){ result.total = 0; return result; }
	var total = contents.match(/font-weight\:bold; font-size\:12px;\"\>Videos (.*?)\/div\>/);
	var total = total[1].match(/ of (.*?)\</);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<TD width=\"150\" height=\"\" valign=\"top\">");

	if(pos != -1){
		contents = contents.substring(pos + 39, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = "http://www.megavideo.com/" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<IMG src\=\"(.*?)\"/);
		var thumb_url = ""; //thumb_url[1];
		//Title
		var title = contents.match(/text-decoration\:none;\"\>(.*?)\<\/a\>/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/color\:#FD0819;  font-size\:12px;\"\>(.*?)\<\/font\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		pos = contents.indexOf("id=\"description_long_");
		var description = contents.substring(pos + 21, contents.length);
		pos = description.indexOf("\n");
		description = description.substring(pos + 4, description.length);
		pos = description.indexOf("</div>");
		var pos2 = description.indexOf("(<a href=\"");
		if(pos2 < pos && pos2 != -1){
			description = splitTo2Lines(ImproveText(description.substring(0, pos2 - 1)), 60);
		} else{
			description = splitTo2Lines(ImproveText(description.substring(0, pos - 3)), 60);
		}
		//Views
		var views = contents.match(/Views\:\<\/font\> \<font class\=\"v5\"\>(.*?)\<\/font\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/Comments\:\<\/font\> \<font class\=\"v5\"\>(.*?)\<\/font\>/);
		var comments = parseInt(comments[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var MegaVideo = new Object();
MegaVideo.Name          = "MegaVideo";		/* Service name */
MegaVideo.Description   = "MegaVideo";		/* Service description */
MegaVideo.SearchDesc    = "MegaVideo";		/* Service search screen description */
MegaVideo.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MegaVideo.CheckURL      = MegaVideo_CheckURL;	/* CheckURL function */
MegaVideo.GetURL        = MegaVideo_GetURL;	/* GetURL function */
MegaVideo.Search        = MegaVideo_Search;	/* Search function */

// add MegaVideo to site list
SiteList.push( MegaVideo );
